package com.sky.validation.mvalidation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MoneyValidator implements ConstraintValidator<Money, BigDecimal> {
    @Override
    public void initialize(Money constraintAnnotation) {

    }

    @Override
    public boolean isValid(BigDecimal value, ConstraintValidatorContext constraintValidatorContext) {
        if (null == value) {
            return false;
        } else {
            String exp = "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$";
            Pattern pattern = Pattern.compile(exp);
            Matcher match=pattern.matcher(value.toString());
            System.out.println(value.toString() + "=======" + match.matches());
            return match.matches();
        }
    }
}
